package com.relytosoft.mizar.media.analyze.services.service.face.impl;

import com.relytosoft.mizar.media.analyze.services.service.face.AyFaceControlService;
import com.relytosoft.mizar.media.analyze.services.service.face.AyFaceDataService;
import com.relytosoft.mizar.media.common.entity.algo.AYVideoSlice;
import com.relytosoft.mizar.media.common.entity.pojo.request.btdl.common.BtdlDataReq;
import com.relytosoft.mizar.media.common.entity.pojo.request.btdl.common.BtdlFaceControlReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


/**
 * @author wss
 * @version 1.0
 * @date 2022/5/16 23:02
 */
@Service
public class AyFaceDataServiceImpl implements AyFaceDataService {

    static ThreadPoolExecutor threadPoolExecutor;

    static {
        threadPoolExecutor = new ThreadPoolExecutor(
                (int) (Runtime.getRuntime().availableProcessors() * 1.5),
                Runtime.getRuntime().availableProcessors() * 3,
                60L,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<Runnable>(10000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
    }

    @Autowired
    private AyFaceControlService ayFaceControlService;

    @Override
    public void receiveRawData(BtdlDataReq dataReq) {
        List<AYVideoSlice> videoSlice = dataReq.getVideoSliceListObject().getVideoSlice();
        for (AYVideoSlice ayVideoSlice : videoSlice) {
            threadPoolExecutor.execute(() ->
                    ayFaceControlService.rawFaceVerify(ayVideoSlice, dataReq.getBigPic()));
        }
    }

    @Override
    public void receiveControlData(List<BtdlFaceControlReq> btdlFaceControlReqs) {
        for (BtdlFaceControlReq b : btdlFaceControlReqs) {
            threadPoolExecutor.execute(() ->
                    ayFaceControlService.controlFaceVerify(b)
            );
        }
    }

}
